//
// Created by wujehy on 2020/5/29.
//

#include "Tool/KeyValueBase.h"

using namespace GeeJoan::DataBase;
std::map<std::string , KeyValueDataType> cacheMap;

/**
 * @brief 测试用户数据
 */
struct TestNode
{
    std::string name;
    int id ;

    virtual ~TestNode()
    {
        std::cout <<" delete user Test Node id = "<<id <<" name = " << name <<std::endl;
    }
};

int main()
{

    KeyValueBase server;


    std::cout<<" size Type : " << sizeof(ValueType)<<std::endl;
    std::cout<<" size ValueDataBaseType : " << sizeof(ValueDataBaseType)<<std::endl;
    std::cout<<" size ValueDataUInt8Type : " << sizeof(ValueDataUInt8Type)<<std::endl;
    std::cout<<" size ValueDataBoolType : " << sizeof(ValueDataBoolType)<<std::endl;
    std::cout<<" size ValueDataStringType : " << sizeof(ValueDataStringType)<<std::endl;
    std::cout<<" size ValueDataInt32Type: " << sizeof(ValueDataInt32Type)<<std::endl;
    std::cout<<" size ValueDataInt64Type : " << sizeof(ValueDataInt64Type)<<std::endl;

    std::string testKey = "testKey";
    int64_t  testInt =11;
    // 引用传递 , 更具参数的有无 选择 传递值的模式
    server.Set(testKey , testInt) ;
    // 拷贝传递
    server.Set2( "test", testInt);
    server.Set2( "test", testInt);
    // 字符
    server.Set2("testStr3" ,"testString3");

    // 指定类型
    server.Set2<double>("testDouble2", 1);

    // 浮点
    server.Set2("testFloat" , 3.14159);
    // bool
    server.Set2("testBool" , true);
    // int64
    server.Set2("testint64" , (int64_t)124587);
    // double
    server.Set2("testDouble1" , (double)3.14159);
    server.Set2("testDouble2" , (float)3.14159);
    server.Set2<double >("testDouble3" , 3.14159);
    server.Set2<float >("testDouble4" , 3.14159);


    //   测试是否是指针 传递
    KeyValueDataTypePort get = server.Get2("test");
    KeyValueDataTypePort get1 = server.Get2("test");
    KeyValueDataTypePort get2 = server.Get2("test");
    KeyValueDataTypePort get3 = server.Get2("test");

    printf(" get addr : %x\n" , get.get());
    printf("get1 addr : %x\n" , get1.get());
    printf("get2 addr : %x\n" , get2.get());
    printf("get3 addr : %x\n" , get3.get());


    //

    // 测试定义 数据结构
    TestNode *test = new TestNode;
    test->id = 1;
    test->name = "dada";

    //  绑定 并设置析构规则
    server.setUserKey("test" , test , [](void *userdata){
        std::cout <<" 用户数据 释放内存中... " << std::endl;
        TestNode *test = (TestNode*)userdata;
        delete test;
    });
    // 测试 key 替换后的指针指针
    printf(" test get : %x , Value = %s \n" , get.get() , get->getValue().c_str());
    ;


    // 遍历数据
    std::cout <<" For Echo Start ... " <<std::endl;

    server.ForEcho([](std::string key, KeyValueDataTypePort port){
        std::cout <<" Key = "<< key <<" , Value = " << port->getValue(true) <<std::endl;
    });

    std::cout <<" For Echo End ... " <<std::endl;

}